package jp.co.dailyReport.dailyReport.sql;

import java.util.Vector;
import java.util.Calendar;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.dailyReport.DailyReport;
import jp.co.dailyReport.Utilities;

/**
 * 日報情報を取得するクラスです。
 */
public class DailyReportLoadForEditExecuter implements Executer {

	private static Logger log = Logger.getLogger(DailyReportLoadForEditExecuter.class);
	private Calendar workDate;
	private int staffId;

	public DailyReportLoadForEditExecuter(Calendar workDate,int staffId) {
		this.workDate = workDate;
		this.staffId = staffId;
	}

	/**
	 * 作業日・社員IDを基に、日報情報を取得します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return 日報オブジェクトの配列
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		ResultSet rset = null;
		PreparedStatement pstmt = null;
		String sql = "select * from dailyReport where workDate=? and staffId=? and delFlg=0";
		log.debug("sql: " + sql);
		Vector answer = new Vector();

		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setDate(1, new Date(workDate.getTime().getTime()));
			pstmt.setInt(2, staffId);
			rset = pstmt.executeQuery();
			DailyReport dailyReport;
			while(rset.next()) {
				dailyReport = new DailyReport();
				dailyReport.setDailyReportId(rset.getInt("dailyReportId"));
				dailyReport.setStaffId(rset.getInt("staffId"));
				dailyReport.setWorkDate(Utilities.getCalendar(rset.getDate("workDate")));
				dailyReport.setWorkingStartHours(Utilities.getCalendar(rset.getTime("workingStartHours")));
				dailyReport.setWorkingEndHours(Utilities.getCalendar(rset.getTime("workingEndHours")));
				dailyReport.setTotalWorkingHours(rset.getDouble("totalWorkingHours"));
				dailyReport.setWorkDetail(Utilities.convUniqueCharacter(rset.getString("workDetail")));
				dailyReport.setProblem(Utilities.convUniqueCharacter(rset.getString("problem")));
				dailyReport.setToDo(Utilities.convUniqueCharacter(rset.getString("toDo")));
				dailyReport.setComment(Utilities.convUniqueCharacter(rset.getString("comment")));
				dailyReport.setUpDay(Utilities.getCalendar(rset.getDate("upDay")));
				answer.add(dailyReport);
			}
		} finally {
			if(rset != null)
			 rset.close();
			if(pstmt != null)
			 pstmt.close();
		}
		return (DailyReport[])answer.toArray(new DailyReport[0]);
	}
}
